﻿using RoadQualityEvaluatorData.Domain.Entities;
using RoadQualityEvaluatorData.DomainServices.Generic;
using RoadQualityEvaluatorData.DomainServices.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace RoadQualityEvalAPI.Helpers
{
    public class PotholeHelper
    {
        private PotholeRepository _potholeRepo;
        private MapSectionRepository _mapSectionRepo;

        public PotholeRepository PotholeRepo
        {
            get
            {
                return this._potholeRepo;
            }
        }

        public MapSectionRepository MapSectionRepo
        {
            get
            {
                return this._mapSectionRepo;
            }
        }

        public PotholeHelper()
        {
            _potholeRepo = new PotholeRepository();
            _mapSectionRepo = new MapSectionRepository();
        }

        public PotholeHelper(UnitOfWork uo)
        {
            _potholeRepo = new PotholeRepository(uo);
            _mapSectionRepo = new MapSectionRepository(uo);
        }

        public Pothole CheckIfPotholeAlreadyExists(Pothole pothole)
        {
            double minimumDistance = 1000;
            Pothole closestExistingPothole = null;
            foreach (var pot in _potholeRepo.GetAll())
            {
                var distanceBetweenPotholes = GeneralHelpers.ComputeDistanceBetweenTwoLocationPoints(pot.Location.Latitude, pot.Location.Longitude, pothole.Location.Latitude, pothole.Location.Longitude);

                if (distanceBetweenPotholes < minimumDistance)
                { 
                    minimumDistance = distanceBetweenPotholes;
                    closestExistingPothole = pot;
                }
            }

            if (minimumDistance <= 10)
                return closestExistingPothole;
            else
                return null;
        }
        
        public void AssignPotholeToMapSection(Pothole pothole)
        {
            double minimumDistance = 1000;
            MapSection closestMapSectionToPothole = new MapSection();

            foreach (var mapSection in _mapSectionRepo.GetAll())
            {
                GeneralHelpers.Point mapSectionProjectionPoint;
                var distanceFromPotholeToMapSection_Rough = GeneralHelpers.GetDistance(new GeneralHelpers.Point(pothole.Location.Latitude, pothole.Location.Longitude), new GeneralHelpers.Point(mapSection.StartLocation.Latitude, mapSection.StartLocation.Longitude), new GeneralHelpers.Point(mapSection.EndLocation.Latitude, mapSection.EndLocation.Longitude), out mapSectionProjectionPoint);
                var distanceFromPotholeToProjectionPoint = GeneralHelpers.ComputeDistanceBetweenTwoLocationPoints(pothole.Location.Latitude, pothole.Location.Longitude, mapSectionProjectionPoint.X, mapSectionProjectionPoint.Y);

                if (distanceFromPotholeToProjectionPoint < minimumDistance)
                {
                    minimumDistance = distanceFromPotholeToProjectionPoint;
                    closestMapSectionToPothole = mapSection;
                }
            }

            if (minimumDistance <= 100)
            {
                closestMapSectionToPothole.Potholes.Add(pothole);
                _mapSectionRepo.Update(closestMapSectionToPothole);
            }
        }


    }
}